package jdbc;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class secretaryjudge
 */
@WebServlet("/src/secretaryjudge")
public class secretaryjudge extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public secretaryjudge() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setHeader("Access-Control-Allow-Origin", "*");//模板
	    response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");//模板
	    response.setHeader("Access-Control-Allow-Headers", "Content-Type");//模板
	    //设置编码格式
	    request.setCharacterEncoding("UTF-8");//模板
	    response.setCharacterEncoding("UTF-8");//模板
		
	    String teacherid = request.getParameter("teacherid");
	    String secretaryid = request.getParameter("secretary_id");
	    String messageid = request.getParameter("messageid");

	    String flag = request.getParameter("flag");

	    
	    
	    hello a = new hello();//模板
	    try {
	    	ResultSet b;
	    	b=a.doselect("SELECT message_content FROM message where message_sender='teacher' AND message_object='secretary' AND message_senderid= ? AND message_objectid=? ", teacherid,secretaryid);
	    	if(b.next()) {
	    		String content=b.getString("message_content");
	    	
	    	var arr=content.split(":");
	    	var s1=arr[1];
	    	var s2=s1.split("课程id");
	    	String coursename=s2[0].trim();
	    	var s3=arr[2];
	    	var s4=s3.split("申请从");

	    	String courseid=s4[0].trim();
	    	var s5=s4[1].split("号教室调为");
	
	    	String oldclassroom=s5[0].trim();
	    	var s6=s5[1].split("号教室");
	    	String classroom=s6[0].trim();
	    	System.out.println(oldclassroom);
	    	System.out.println(classroom);
	    	String newcontent="课程名称: "+coursename+" 课程id: "+courseid+" 已经从 "+oldclassroom+" 号教室调为 "+classroom+" 号教室";
	    	String newcontent1="已经同意关于 "+"课程名称: "+coursename+" 课程id: "+courseid+" 从 "+oldclassroom+" 号教室调为 "+classroom+" 号教室的申请";
	    	String newcontent2="不同意关于 "+"课程名称: "+coursename+" 课程id: "+courseid+" 从 "+oldclassroom+" 号教室调为 "+classroom+" 号教室的申请";

//	    	var room=classroom.split(" ");
	    	if(flag.equals("true")) {
	    		
	    		a.doupdate("INSERT INTO message (message_object,message_objectid,message_content,message_sender,message_senderid) VALUES ('teacher', ?, ? , 'secretary', ?) ",teacherid,newcontent1,secretaryid);
			a.doupdate("UPDATE course SET course_classroom= ? where course_id= ? ",classroom,courseid);
			a.doupdate("DELETE FROM message where message_id= ? ",messageid);
	    	ResultSet c;
	    	c=a.doselect("SELECT student_id from sc where course_id= ? ", courseid);
	    	while(c.next()) {
	    		String studentid=c.getString("student_id");
	    		a.doupdate("INSERT INTO message (message_object,message_objectid,message_content,message_sender,message_senderid) VALUES ('student', ?, ? , 'secretary', ?) ",studentid,newcontent,secretaryid);
	    	}
	    	c.close();
	    	}
	    	else {
	    		a.doupdate("INSERT INTO message (message_object,message_objectid,message_content,message_sender,message_senderid) VALUES ('teacher', ?, ? , 'secretary', ?) ",teacherid,newcontent2,secretaryid);
				a.doupdate("DELETE FROM message where message_id= ? ",messageid);

	    	}
			response.getWriter().write("true");
	    	}
	    	b.close();

	    }catch (SQLException e) {//模板
			e.printStackTrace();//模板
		}//模板
	    }

}
